app.factory('priceDetailView', [function() {
    var priceDetailView = {};

    var defaultTime = "2100-01-01 00:00:00";
    var beginTime = "";
    var endTime = "";

    priceDetailView.initCalander = function(){
        $('#begin-time').datetimepicker({
            timeFormat: "HH:mm:ss",
            dateFormat: "yy-mm-dd",
            showSecond: true,
            changeYear: true,
            changeMonth: true,
            maxDate: defaultTime,
            beforeShow: function() {
                beginTime = $("#begin-time").val();
                if($.trim($('#end-time').val())) {
                    var endTimeData = new Date($("#end-time").val()).getTime();
                    $("#begin-time").datetimepicker("option", "maxDate", new Date(endTimeData - 1000).format("yyyy-MM-dd hh:mm:ss"));
                } else {
                    $("#begin-time").datetimepicker("option", "maxDate", "");
                }
                $("#begin-time").val(beginTime);
            },
            onSelect: function(date) {
                var beginTimeData = new Date(date).getTime();
                var endTimeData = new Date($("#end-time").val()).getTime();
                if(endTimeData <= beginTimeData && $.trim($('#end-time').val())) {
                    $("#begin-time").val(new Date(endTimeData - 1000).format("yyyy-MM-dd hh:mm:ss"));
                    $("#begin-time").datetimepicker("refresh");
                    beginTime = $("#begin-time").val();
                } else {
                    beginTime = date;
                }
            },
            onClose: function() {
                $("#begin-time").datetimepicker("setDate", beginTime);
                $("#begin-time").val(beginTime);
            }
        });

        $('#end-time').datetimepicker({
            timeFormat: "HH:mm:ss",
            dateFormat: "yy-mm-dd",
            showSecond: true,
            changeYear: true,
            changeMonth: true,
            maxDate: defaultTime,
            beforeShow: function() {
                endTime = $("#end-time").val();

                if($.trim($('#begin-time').val())) {
                    var beginTimeData = new Date($("#begin-time").val()).getTime();
                    $("#end-time").datetimepicker("option", "minDate", new Date(beginTimeData + 1000).format("yyyy-MM-dd hh:mm:ss"));
                } else {
                    $("#end-time").datetimepicker("option", "minDate", "");
                }
                $("#end-time").val(endTime);
            },
            onSelect: function(date) {
                var beginTimeData = new Date($("#begin-time").val()).getTime();
                var endTimeData = new Date(date).getTime();
                if(endTimeData <= beginTimeData && $.trim($('#begin-time').val())) {
                    $("#end-time").val(new Date(beginTimeData + 1000).format("yyyy-MM-dd hh:mm:ss"));

                    $("#end-time").datetimepicker("refresh");
                    endTime = $("#end-time").val();
                } else {
                    endTime = date;
                }
            },
            onClose: function() {
                $("#end-time").datetimepicker("setDate", endTime);
                $("#end-time").val(endTime);
            }
        });
    };

    /**
     * 计算滚动条高度
     * @param type
     * @returns {number}
     */
    priceDetailView.height = function(type){
        var height = 0;
        switch (type){
            case 'table': height = $('#m-show-region').height() - 185;
                break;
        }
        return height;
    };

    /**
     * 滚动条设置
     * @param elm    选择器，#id 或 .class
     * @param height    高度
     */
    priceDetailView.slimscroll = function(elm, height){
        $(elm).slimscroll({ height: height });
    };

    return priceDetailView;
}]);